home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8674 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  74 lines

  1. Path: news.primenet.com!not-for-mail
  2. From: gbe@primenet.com (Gary Edstrom)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: What is referencing good for?
  5. Date: 25 Feb 1996 16:47:00 -0700
  6. Organization: Sequoia Software
  7. Sender: root@primenet.com
  8. Message-ID: <3130e9f1.149228054@news.primenet.com>
  9. References: <4goojd$a9g@wintermute.ecs.fullerton.edu> <4goutn$31u@news.bridge.net> <3130c026.8571250@news2.cts.com>
  10. X-Posted-By: ip097.lax.primenet.com
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. lboard@cts.com (Larry Board) wrote:
  14.  
  15. >On 25 Feb 1996 06:17:27 GMT, David Byrden <100101.2547@compuserve.com>
  16. >wrote:
  17. >
  18. >> References are necessary to allow operator overloading.
  19. >>
  20. >>class array {
  21. >>private:
  22. >>    int * dataBlock ;
  23. >>public :
  24. >>    int& operator[] ( int index )
  25. >>    {
  26. >>        return dataBlock [ index ] ;
  27. >>    }
  28. >>    // constructor etc etc
  29. >>} ;
  30. >>
  31. >>    array  ar ;
  32. >>    ar[4] = 0 ;      // can't be done with pointers
  33. >
  34. >Good example, but.... the next question begs to be answered.  Why
  35. >would anybody want to do this?  It essentially gives public access to
  36. >datablock, which is a private variable.
  37.  
  38. There are many reasons for doing this.  For one, I have created an array class
  39. that does bounds checking at run time.  In addition to a pointer to the array,
  40. I also store the size of the array within the class.  I do not need to
  41. explicity check the index into the array before fetching or storing a value.  I
  42. have the program throw an exception if the index is out of bounds.  My array
  43. class would look as follows:
  44.  
  45. class array {
  46.     private:
  47.         int size;
  48.         int * dataBlock;
  49.     public:
  50.         int operator [] (int offset) const 
  51.             {
  52.             if (offset >= size) throw exception;
  53.             return dataBlock[offset];
  54.             }
  55.         int & operator [] (int offset)
  56.             {
  57.             if (offset >= size) throw exception;
  58.             return dataBlock[offset];
  59.             }
  60.     };
  61.  
  62. Of course, there are other members that you need to add for a complete
  63. implementation of the array class.  The first version of "operator []" is
  64. called when the array appears on the right side of an assignment statement.
  65. The second version is called when the array appears on the left side.
  66.  
  67. Gary Edstrom
  68.  
  69. --
  70. Gary Edstrom <gbe@primenet.com> | Sequoia Software
  71. PO Box 9573                     | Programming & Technical Services
  72. Glendale CA 91226-0573          | PGP Key ID: 0x1A0D44BD
  73. PGP Fingerprint: 72 AA 4F 73 05 53 89 C6  8A EE F4 EE D1 C0 13 8D 
  74.